package dept

import (
	"context"
	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/text/gstr"
	"github.com/gogf/gf/v2/util/gconv"
	"ruoyi-vue-pro-go/api/common"
	v1 "ruoyi-vue-pro-go/api/dept/v1"
	"ruoyi-vue-pro-go/internal/dao"
	"ruoyi-vue-pro-go/internal/model/do"
	"ruoyi-vue-pro-go/internal/model/entity"
	"ruoyi-vue-pro-go/internal/service"
)

type (
	sDept struct{}
)

func init() {
	service.RegisterDept(New())
}

func New() service.IDept {
	return &sDept{}
}

func (s *sDept) GetSimpleList(ctx context.Context, req *v1.GetSimpleListReq) (res *v1.GetSimpleListRes, err error) {
	res = &v1.GetSimpleListRes{}
	err = dao.SystemDept.Ctx(ctx).Scan(&res.List)
	return
}

func (s *sDept) GetPage(ctx context.Context, req *v1.GetPageReq) (res *v1.GetPageRes, err error) {
	res = &v1.GetPageRes{
		List:          make([]*entity.SystemDept, 0),
		CommonPageRes: &common.CommonPageRes{},
	}
	model := dao.SystemDept.Ctx(ctx)
	if req.PageSize != 0 {
		res.PageNo = req.PageNo
		res.PageSize = req.PageSize
		model = model.Page(req.PageNo, req.PageSize)
	}
	err = model.ScanAndCount(&res.List, &res.Total, false)
	return
}

func (s *sDept) GetList(ctx context.Context, req *v1.GetListReq) (res *v1.GetListRes, err error) {
	res = &v1.GetListRes{}
	err = dao.SystemDept.Ctx(ctx).Scan(&res.List)
	return
}

func (s *sDept) Update(ctx context.Context, req *v1.UpdateReq) (res *v1.UpdateRes, err error) {
	res = &v1.UpdateRes{}
	// 这里需要获取当前用户名和租户id
	r := ghttp.RequestFromCtx(ctx)
	username := r.GetCtxVar("username").String()
	tenantId := gconv.Int64(r.Header.Get("Tenant-Id"))
	req.TenantId = tenantId
	req.Creator = username
	req.Updater = username
	_, err = dao.SystemDept.Ctx(ctx).Data(req).Where(do.SystemDept{
		Id: req.Id,
	}).Update()
	if err != nil {
		return nil, gerror.NewCode(gcode.CodeDbOperationError, "dept update failed")
	}
	return
}

func (s *sDept) Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) {
	res = &v1.CreateRes{}
	// 这里需要获取当前用户名和租户id
	r := ghttp.RequestFromCtx(ctx)
	username := r.GetCtxVar("username").String()
	tenantId := gconv.Int64(r.Header.Get("Tenant-Id"))
	req.TenantId = tenantId
	req.Creator = username
	req.Updater = username
	var cls = dao.SystemDept.Columns()
	count, err := dao.SystemDept.Ctx(ctx).
		Where(cls.Name, req.Name).Count()
	if err != nil {
		return res, err
	}
	if count > 0 {
		return res, gerror.NewCode(gcode.CodeDbOperationError, "岗位已存在")
	}
	_, err = dao.SystemDept.Ctx(ctx).Data(req).Insert()
	if err != nil {
		return res, gerror.NewCode(gcode.CodeDbOperationError, "dept create failed")
	}
	return
}

func (s *sDept) Delete(ctx context.Context, req *v1.DeleteReq) (res *v1.DeleteRes, err error) {
	res = &v1.DeleteRes{}
	var cls = dao.SystemDept.Columns()
	_, err = dao.SystemDept.Ctx(ctx).Where(cls.Id, req.Id).Delete()
	if err != nil {
		return nil, gerror.NewCode(gcode.CodeDbOperationError, "dept delete failed")
	}
	return
}

func (s *sDept) DeleteList(ctx context.Context, req *v1.DeleteListReq) (res *v1.DeleteListRes, err error) {
	res = &v1.DeleteListRes{}
	var cls = dao.SystemDept.Columns()
	// 参数转换
	ids := gconv.Ints(gstr.Split(req.Ids, ","))
	if len(ids) == 0 {
		return nil, gerror.New("无效的ID参数")
	}
	_, err = dao.SystemDept.Ctx(ctx).WhereIn(cls.Id, ids).Delete()
	if err != nil {
		return nil, gerror.NewCode(gcode.CodeDbOperationError, "dept delete list failed")
	}
	return
}
